package com.maxatime.service.impl;

import com.maxatime.dao.EmployeeInfoMapper;
import com.maxatime.dao.EmployeeRegisterMapper;
import com.maxatime.dao.StudentRegisterMapper;
import com.maxatime.dao.studentInformationMapper;
import com.maxatime.pojo.EmployeeInfo;
import com.maxatime.pojo.EmployeeRegister;
import com.maxatime.pojo.StudentRegister;
import com.maxatime.pojo.studentInformation;
import com.maxatime.service.ERegisterService;
import com.maxatime.utils.MyContent;
import com.maxatime.utils.PasswordManagerUtil;
import com.maxatime.utils.SerialNumberUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ERegisterServiceImpl implements ERegisterService {

    Logger log = LogManager.getLogger();

    @Autowired
    //注册老师表(注册员工账号密码表)
    private EmployeeRegisterMapper employeeRegisterMapper;

    @Autowired
    //注册老师信息表(员工具体的表)
    private EmployeeInfoMapper employeeInfoMapper;

    @Autowired
    //注册学生表(注册用户账号密码表)
    private StudentRegisterMapper studentRegisterMapper;

    @Autowired
    //注册学生信息表(用户具体的表)
    private studentInformationMapper studentInformationMapper;


    @Override
    public EmployeeRegister login(EmployeeRegister employeeRegister) {
        //老师登录

        EmployeeRegister e = employeeRegisterMapper.findLogin(employeeRegister);
        return e;
    }

    @Override
    public StudentRegister loginStu(StudentRegister studentRegister) {
        //学生登录
        StudentRegister s = studentRegisterMapper.findLogin(studentRegister);
        return s;
    }

    @Override
    public void inse(List<studentInformation> readResult) {
        studentRegisterMapper.inse(readResult);
    }


    @Override
    public Map<String, Object> saveRegister(String username, String email, String password,Integer judge) {

        //生成一个随机的userId // 员工ID/学生ID
        String userId = SerialNumberUtil.getUUID();

        // 密码,md5加密
        String passMD5 = PasswordManagerUtil.getMd5DigestAsHex(password);

        //注册表
        int iReg = 0;

        //员工表/老师表的
        int iInfo = 0;

        //如果judge == 1 的话，那么注册的就是老师
        if (judge == 1) {

            // 老师注册表
            EmployeeRegister employeeRegister = new EmployeeRegister();
                employeeRegister.setEmail(email);
                employeeRegister.setPassword(passMD5);
                employeeRegister.setUserId(userId);

            log.info(employeeRegister);
            iReg = employeeRegisterMapper.insertSelective(employeeRegister);
            log.info("insertSelective-iReg::: " + iReg);

            // 员工信息表
            EmployeeInfo employeeInfo = new EmployeeInfo();
                employeeInfo.setEmployeeName(username);
                employeeInfo.setEmail(email);
                employeeInfo.setUserId(userId);
                //设置employee_no老师的 1
                employeeInfo.setEmployeeNo("1");
            log.info("insertSelective-employeeInfo::: " + employeeInfo);

            iInfo = employeeInfoMapper.insertSelective(employeeInfo);
            log.info("insertSelective-iReg::: " + iInfo);

            Map<String, Object> m = new HashMap<>();

            m.put("code","0000");
            m.put("msg","交易成功");


        }
        //如果judge == 2 的话，那么注册的就是学生
        if (judge == 2) {

            //学生注册表
            StudentRegister stuReg = new StudentRegister();
                    stuReg.setEmail(email);
                    stuReg.setPassword(passMD5);
                    stuReg.setStudentId(userId);
                    stuReg.setStatus("0");
                    stuReg.setDeleteFlag("0");
                Calendar calendar = Calendar.getInstance();
                    stuReg.setCreateTime(calendar.getTime());
                    stuReg.setUpdateTime(calendar.getTime());

            log.info("学生注册"+stuReg);
            iReg = studentRegisterMapper.insertSelective(stuReg);
            log.info("studentRegisterMapper---insertSelective:::" + iReg);

//            Calendar calendar = Calendar.getInstance();
//            SimpleDateFormat format = new SimpleDateFormat("YY-MM-DD HH:MM:SS");//日期格式:年-月-日 时-分-秒
//            goodsIn.setPurchaseDate(calendar.getTime());//goodsIn

            studentInformation stuInfo = new studentInformation();
                stuInfo.setStuId(userId);
                stuInfo.setStuName(username);
                stuInfo.setDeleteFlag(0);
                stuInfo.setCreateDate(calendar.getTime());
                stuInfo.setUpdateDate(calendar.getTime());

            log.info("学生信息"+stuInfo);
            iInfo = studentInformationMapper.insertSelective(stuInfo);
            log.info("studentRegisterMapper---insertSelective:::" + iReg);
        }

        Map<String, Object> m = new HashMap<>();
        if (iReg >0 & iInfo > 0){
            //注册   员工/学生
            log.info("注册成功");
            m.put(MyContent.CODE, "0000");
            m.put(MyContent.MSG, "注册成功！");
        }else {
            log.info("注册失败");
            m.put(MyContent.CODE, "9999");
            m.put(MyContent.MSG,"注册失败！");
        }





        return m;
    }




}
